<?php

namespace App;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;

/**
 * App\AgentTeam
 *
 * @property int $id
 * @property int $agent_user_id agent_user_id
 * @property int $son_agent_user_id son_agent_user_id
 * @property int $son_user_id son_user_id
 * @property \Illuminate\Support\Carbon|null $deleted_at
 * @property \Illuminate\Support\Carbon|null $created_at
 * @property \Illuminate\Support\Carbon|null $updated_at
 * @method static bool|null forceDelete()
 * @method static \Illuminate\Database\Eloquent\Builder|\App\AgentTeam newModelQuery()
 * @method static \Illuminate\Database\Eloquent\Builder|\App\AgentTeam newQuery()
 * @method static \Illuminate\Database\Query\Builder|\App\AgentTeam onlyTrashed()
 * @method static \Illuminate\Database\Eloquent\Builder|\App\AgentTeam query()
 * @method static bool|null restore()
 * @method static \Illuminate\Database\Eloquent\Builder|\App\AgentTeam whereAgentUserId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\AgentTeam whereCreatedAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\AgentTeam whereDeletedAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\AgentTeam whereId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\AgentTeam whereSonAgentUserId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\AgentTeam whereSonUserId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\AgentTeam whereUpdatedAt($value)
 * @method static \Illuminate\Database\Query\Builder|\App\AgentTeam withTrashed()
 * @method static \Illuminate\Database\Query\Builder|\App\AgentTeam withoutTrashed()
 * @mixin \Eloquent
 */
class AgentTeam extends Model
{
    //
    use SoftDeletes;
    public static function join($son_user_id,$father_user_id = 0){
        if ($son_user_id == 0)
            return 0;
        if ($father_user_id == 0)
            return 0;
        $father_agent = AgentList::whereUserId($father_user_id)->first();
        if (!$father_agent){
            //B->C
            $is_join = AgentTeam::whereSonUserId($son_user_id)->first();
            $is_joinB = AgentTeam::whereSonAgentUserId($son_user_id)->first();
            if ($is_join)
                return 0;
            if ($is_joinB)
                return 0;
            $is_son_agent = AgentTeam::whereSonAgentUserId($father_user_id)->first();
            if ($is_son_agent){
                //B->C
                $record = new AgentTeam();
                $record->agent_user_id  = $is_son_agent->agent_user_id;
                $record->son_agent_user_id = $is_son_agent->son_agent_user_id;
                $record->son_user_id = $son_user_id;
                $record->save();
                return 1;
            }
        } else {
//            A->B
            $is_joinB = AgentTeam::whereSonAgentUserId($son_user_id)->first();
            $is_joinC = AgentTeam::whereSonUserId($son_user_id)->first();
            if ($is_joinB)
                return 0;
            if ($is_joinC)
                return 0;
//            $is_agent = AgentTeam::whereAgentUserId($father_user_id)->first();
//            if ($is_agent){
                //A->B
                $record = new AgentTeam();
                $record->agent_user_id      = $father_user_id;
                $record->son_agent_user_id  = $son_user_id;
                $record->save();
                return 1;
//            }
        }
        return 0;
    }
    public static function getSonUser($agent_user_id){
        $father_agent = AgentList::whereUserId($agent_user_id)->first();
        if ($father_agent){
            //A
            $data = AgentTeam::whereAgentUserId($agent_user_id)->whereSonUserId(0)->get();
        } else{
            //B
            $data = AgentTeam::whereSonAgentUserId($agent_user_id)->get();
        }
        if (count($data) > 0){
            foreach ($data as $item){
                $item->agent_user_info = UserInfo::getUserInfo($item->agent_user_id);
                $item->son_agent_user_info = UserInfo::getUserInfo($item->son_agent_user_id);
                if ($item->son_user_id)
                    $item->son_user_info = UserInfo::getUserInfo($item->son_user_id);
                else
                    $item->son_user_info = '';
            }
        }
        return $data;
    }

    public static function getAgentUserOrder($agent_user_id = 0){
        $userIds = [];
        if ($agent_user_id){
            //get this user's data.
            $father_agent = AgentList::whereUserId($agent_user_id)->first();
            if ($father_agent){
//                A
                $sonIds = AgentTeam::whereAgentUserId($agent_user_id)->get();
                if (count($sonIds) > 0){
                    foreach ($sonIds as $item){
                        if ($item->son_agent_user_id){
                            array_push($userIds,$item->son_agent_user_id);
                        }
                        if ($item->son_user_id){
                            array_push($userIds,$item->son_user_id);
                        }
                    }
                }
            } else {
//                B
                $sonIds = AgentTeam::whereSonAgentUserId($agent_user_id)->get();
                if (count($sonIds) > 0){
                    foreach ($sonIds as $item){
                        if ($item->son_user_id){
                            array_push($userIds,$item->son_user_id);
                        }
                    }
                }
            }
        } else {
            //get all users' data.
           //TODO
        }
    }
}
